package bTree.LeetCode;

import java.util.ArrayList;
import java.util.List;

public class 汇总区间 {
    public List<String> summaryRanges(int[] nums) {
        List<String> target=new ArrayList<>();
        if(nums.length==1){
            target.add(Integer.toString(nums[0]));
            return target;
        }
        int left=0,right=1;
        while (right<=nums.length-1){
            if(nums[right]!=nums[right-1]+1){
                if(right-1==left){
                    target.add(Integer.toString(nums[left]));
                }else {
                    target.add(Integer.toString(nums[left])+"->"+Integer.toString(nums[right-1]));
                }
                if(right==nums.length-1){
                    target.add(Integer.toString(nums[right]));
                    break;
                }
                left=right;
                right=right+1;
                if(right==nums.length-1){
                  if(nums[right]-1==nums[left]){
                      target.add(Integer.toString(nums[left])+"->"+Integer.toString(nums[right]));
                  }else{
                      target.add(Integer.toString(nums[left]));
                      target.add(Integer.toString(nums[right]));
                  }
                    break;
                }
            }else{
                if(right==nums.length-1){
                    target.add(Integer.toString(nums[left])+"->"+Integer.toString(nums[right]));
                    break;
                }
                right++;
            }

        }
return target;
    }
}
